#pragma

#include<iostream>
#include<algorithm>

using namespace std;

namespace wx
{
	template<class K>
	struct BinarySerachTreeNode
	{
		K _key;
		BinarySerachTreeNode<K>* _left;
		BinarySerachTreeNode<K>* _right;

		BinarySerachTreeNode(const K& key)
			:_left(nullptr),
			_right(nullptr),
			_key(key)
		{}
	};

	template<class K>
	class BSTree
	{
		typedef BinarySerachTreeNode<K> Node;
	public:
		BSTree() = default;

		BSTree(const BSTree<K>& t)
		{
			_root = Copy(t._root);
		}

		BSTree<K>& operator=(BSTree<K> tem)
		{
			swap(tem);
			return *this;
		}

		~BSTree()
		{
			Destroy(_root);
			_root = nullptr;
		}

		void swap(BSTree<K>& t)
		{
			std::swap(_root, t._root);
		}

		void inorder()
		{
			_inorder(_root);
			return;
		}

		bool insert(K key)
		{
			if (_root == nullptr)
			{
				_root = new Node(key);
				return true;
			}
			Node* cur = _root;
			Node* parent = nullptr;
			while (cur)
			{
				if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					return false;
				}
			}

			cur = new Node(key);

			if (parent->_key > key)
			{
				parent->_left = cur;
			}
			if (parent->_key < key)
			{
				parent->_right = cur;
			}
			return true;
		}

		bool find(K key)
		{
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key < key)
				{

					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					cur = cur->_left;
				}
				else
				{
					return true;
				}
			}

			return false;
		}

		bool erase(const K& key)
		{
			Node* parent = nullptr;
			Node* cur = _root;
			while (cur)
			{
				if (cur->_key < key)
				{
					parent = cur;
					cur = cur->_right;
				}
				else if (cur->_key > key)
				{
					parent = cur;
					cur = cur->_left;
				}
				else
				{
					if (cur->_left == nullptr)
					{
						if (cur == _root)
						{
							_root = cur->_right;
						}
						else
						{
							if (cur == parent->_left)
							{
								parent->_left = cur->_right;
							}
							else
							{
								parent->_right = cur->_right;
							}
						}
						delete cur;
					}
					else if (cur->_right == nullptr)
					{
						if (cur == _root)
						{
							_root = cur->_left;
						}
						else
						{
							if (cur == parent->_left)
							{
								parent->_left = cur->_left;
							}
							else
							{
								parent->_right = cur->_left;
							}
						}
						delete cur;
					}
					else
					{
						Node* leftparent = cur;
						Node* leftmax = cur->_left;
						while (leftmax->_right)
						{
							leftparent = leftmax;
							leftmax = leftmax->_right;
						}
						std::swap(leftmax->_key, cur->_key);
						if (leftparent->_right!=leftmax)
						{
							leftparent->_left = leftmax->_left;
						}
						else
						{
							leftparent->_right = leftmax->_left;
								
						}
						delete leftmax;
						
					}
					return true;
				}

			}
			return false;
		}

	private:

		void Destroy(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}

			Destroy(root->_left);
			Destroy(root->_right);
			delete root;
		}

		void _inorder(Node* root)
		{
			if (root == nullptr)
			{
				return;
			}
			_inorder(root->_left);
			cout << root->_key << " ";
			_inorder(root->_right);
		}
		Node* Copy(Node* root)
		{
			if (root == nullptr)
			{
				return nullptr;
			}

			Node* newroot = new Node(root->_key);
			newroot->_left = Copy(root->_left);
			newroot->_right = Copy(root->_right);
			return newroot;

		}

		Node* _root;
	};
}

